package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;

/**
 * Write a description of class ProductUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProductUI
{
    // instance variables - replace the example below with your own
    private ProductNoUI pNO;
    private ProductController pCtr;

    /**
     * Constructor for objects of class ProductUI
     */
    public ProductUI()
    {
        // initialise instance variables
        pCtr = ProductController.getInstance();
    }

    public void start()
    {
        Menu();

    }

    public void Menu()
    {
        boolean exit = false;

        while(!exit)  //if exit = true, the main menu will shut down 
        {
            int choice = writeMainMenu();
            switch(choice)
            {
                case 1:
                create();
                break;
                case 2:
                read();
                break;
                case 3:
                update();
                break;                
                case 4:
                delete();
                break;
                case 5:
                printID();
                break;
                case 6:
                printAll();
                break;
                case 7:
                startProductNo();
                break;
                default:                
                exit = true;
                break; 
            }
        }
    }

    public int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ****Store Menu***");
        System.out.println("  (1) Create");
        System.out.println("  (2) Read");
        System.out.println("  (3) Update");
        System.out.println("  (4) Delete");
        System.out.println("  (5) Print ID");
        System.out.println("  (6) Print ALL");
        System.out.println("  (7) Add products to Store");
        System.out.println("  (8) Return");
        System.out.println("\n make your choice: ");

        int choice = keyboard.nextInt();

        return choice;

    }

    public void startProductNo()
    {
        pNO = new ProductNoUI();

        pNO.start();
    }

    private long inputID()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        long out = 0;        
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product Description ID: " );
        while(!ok)
        {
         try{
                out = keyboard.nextLong();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }
        return out;
    }  

    private double inputPrice()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        double out = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Price: " );
        while(!ok)
        {   
            try{
                out = keyboard.nextDouble();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }    

        return out;
    }

    private int inputMinNo()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int out = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type minimum no: //minimum number on store" );
        while(!ok)
        {   
            try{
                out = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }    

        return out;
    }

    private String inputDescription()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Description: " );
        String out = keyboard.nextLine();
        return out;
    }

    public void create()
    {
        Scanner keyboard = new Scanner(System.in);       

        pCtr.createProduct(inputDescription(), inputPrice(), inputMinNo());
        long id = pCtr.getID();

        System.out.println("     new Product Description has been created - se info above");
        pCtr.printOneProduct(id);
        System.out.println();        
        System.out.println("press any to continue");
        keyboard.nextLine();
    }   

    public void read()
    {

        Scanner keyboard = new Scanner(System.in);
        long id = inputID();

        if(pCtr.readProduct(id) != null)
        {
            System.out.println("    Product Description was found - se info above");            
            pCtr.printOneProduct(id);
            System.out.println("  Product code nr.: "+pCtr.readProduct(id));
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void update()
    {
        Scanner keyboard = new Scanner(System.in);
        long id = inputID();

        if(pCtr.readProduct(id) != null)
        {
            pCtr.updateProduct(id, inputDescription(), inputPrice(), inputMinNo());

            System.out.println("      Product has been updated - se info above");
            pCtr.printOneProduct(id);
            System.out.println("  Product Description code nr.: "+pCtr.readProduct(id));
        }
        else
        {
            System.out.println("      Product Description was not found - try another ID");
        }
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void delete()
    {
        Scanner keyboard = new Scanner(System.in);

        long id = inputID();
        if(pCtr.readProduct(id) != null)
        {
            System.out.println("        Product Description is deleted - se info above");
            pCtr.printOneProduct(id);
            pCtr.deleteProduct(id);
        }
        else
        {
            System.out.println("        Product Description was not found - try another ID");
        }
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printID()
    {
        Scanner keyboard = new Scanner(System.in);

        long id = inputID();

        if(pCtr.readProduct(id) != null)
        {       
                pCtr.printOneProduct(id);            
        }
        else
        {
            System.out.println("        Product Description was not found - try another ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printAll()
    {
        Scanner keyboard = new Scanner(System.in);

        if(pCtr.getPdSize() > 0)
        {
            pCtr.printAllProduc();
        }
        else
        {
            System.out.println("        No Product Description was found - create a Description");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }
}
